package org.mspring.mlog.duoshuo.sync;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 当action为create时，meta为object。为新创建的评论信息。下面为评论信息参数
 *
 * @author GaoYoubo
 * @since 2015/2/19
 */
public class CreateMeta {
    private long postId; //评论id。请注意，post_id为64位二进制整数，MySQL数据类型建议定义为bigInt。
    private long threadId; //文章在多说的id。请注意，thread_id为64位二进制整数，MySQL数据类型建议定义为bigInt。
    private String threadKey; //文章在原站点的文章标识。无记录时，为NULL；
    private long authorId; //作者在多说的id。0表示匿名用户。
    private String authorName; //作者显示名。有可能为空。
    private String authorEmail; //作者邮箱。有可能为空字符串。
    private String authorUrl; //作者网址。有可能为空字符串。
    private String authorKey; //作者在网站中对应的id。有可能为0。
    private String ip; //作者ip地址。格式为*.*.*.*
    private Date createdAt; //评论创建日期，格式示例：2012-07-13T21:58:13+08:00。
    private String message; //评论内容。请注意截取。
    private String status; //评论状态。创建评论时，可能的状态：approved：已经通过；pending：待审核；spam：垃圾评论。
    private long parentId; //父评论id。请注意，parent_id为64位二进制整数，MySQL数据类型建议定义为bigInt。
    private String type; //类型。现在均为空。

    public static CreateMeta fromJsonString(String str) throws ParseException {
        JSONObject object = JSON.parseObject(str);

        CreateMeta meta = new CreateMeta();
        meta.setPostId(object.getLongValue("post_id"));
        meta.setThreadId(object.getLongValue("thread_id"));
        meta.setThreadKey(object.getString("thread_key"));
        meta.setAuthorId(object.getLongValue("author_id"));
        meta.setAuthorName(object.getString("author_name"));
        meta.setAuthorEmail(object.getString("author_email"));
        meta.setAuthorKey(object.getString("author_key"));
        meta.setIp(object.getString("ip"));
        meta.setCreatedAt(parseDate(object.getString("created_at")));
        meta.setMessage(object.getString("message"));
        meta.setStatus(object.getString("status"));
        meta.setParentId(object.getLongValue("parent_id"));
        meta.setType(object.getString("type"));
        return meta;
    }

    private static Date parseDate(String input) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        return sdf.parse(input);
    }


    public long getPostId() {
        return postId;
    }

    public void setPostId(long postId) {
        this.postId = postId;
    }

    public long getThreadId() {
        return threadId;
    }

    public void setThreadId(long threadId) {
        this.threadId = threadId;
    }

    public String getThreadKey() {
        return threadKey;
    }

    public void setThreadKey(String threadKey) {
        this.threadKey = threadKey;
    }

    public long getAuthorId() {
        return authorId;
    }

    public void setAuthorId(long authorId) {
        this.authorId = authorId;
    }

    public String getAuthorName() {
        return authorName;
    }

    public void setAuthorName(String authorName) {
        this.authorName = authorName;
    }

    public String getAuthorEmail() {
        return authorEmail;
    }

    public void setAuthorEmail(String authorEmail) {
        this.authorEmail = authorEmail;
    }

    public String getAuthorUrl() {
        return authorUrl;
    }

    public void setAuthorUrl(String authorUrl) {
        this.authorUrl = authorUrl;
    }

    public String getAuthorKey() {
        return authorKey;
    }

    public void setAuthorKey(String authorKey) {
        this.authorKey = authorKey;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public Date getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(Date createdAt) {
        this.createdAt = createdAt;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public long getParentId() {
        return parentId;
    }

    public void setParentId(long parentId) {
        this.parentId = parentId;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
}
